﻿using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using System.Windows.Media;
using System.Xml.Linq;

namespace 花店管理系统.geren
{
    public partial class Quanping : Form
    {
        List<string> commentsList;
        List<int> ratingsList; // 新增用于保存每条评论的评分
        int currentCommentIndex = 0;
        private PictureBox[] stars;
        private const int STAR_SIZE = 70;
        public Quanping()
        {
            InitializeComponent();
            InitializeStars();
            LoadUserComments();
            UpdateCommentDisplay();
        }

        private void InitializeStars()
        {
            stars = new PictureBox[5];
            for (int i = 0; i < 5; i++)
            {
                PictureBox star = new PictureBox
                {
                    Image = System.Drawing.Image.FromFile(@"D:\大二下\数据库编程\花店管理系统\花店管理系统\bin\Debug\按键图标\评分星2.jpg"), // 注意使用@符号创建原始字符串
                    Location = new System.Drawing.Point(i * STAR_SIZE, 0),
                    Size = new Size(STAR_SIZE, STAR_SIZE),
                    Tag = i
                };
                panel1.Controls.Add(star); // 将星星添加到panel1控件中
                stars[i] = star;
                star.SizeMode = PictureBoxSizeMode.Zoom;
            }
        }

        private void Quanping_Load(object sender, EventArgs e)
        {
            LoadUserComments();
            UpdateCommentDisplay();
        }

        private void LoadUserComments()
        {
            string currentUser = Tproducts.Pname;
            string query = @"
            
            SELECT Torder.O_pinglun AS Comment, Torder.O_dafen AS Rating
            FROM Torder
            INNER JOIN Tgood ON Torder.O_huaduoming = Tgood.G_name
            WHERE Tgood.G_name = '" + currentUser + "'";
            System.Data.DataTable commentsTable = DB.GetDataSet(query);
            commentsList = new List<string>();
            ratingsList = new List<int>(); // 初始化评分列表
            foreach (DataRow row in commentsTable.Rows)
            {
                commentsList.Add(row["Comment"].ToString());
                if (!Convert.IsDBNull(row["Rating"]))
                {
                    ratingsList.Add(Convert.ToInt32(row["Rating"]));
                }
                else
                {
                    ratingsList.Add(0); // 如果评级为空，则默认设为0
                }
            }
        }

        private void UpdateCommentDisplay()
        {
            if (commentsList.Count > 0 && currentCommentIndex >= 0 && currentCommentIndex < commentsList.Count)
            {
                textBox1.Text = commentsList[currentCommentIndex];
                DisplayRatings(ratingsList[currentCommentIndex]); // 显示当前评论的评分
            }
            else
            {
                textBox1.Text = "没有更多评论";
            }
        }

        private void DisplayRatings(int rating)
        {
            for (int i = 0; i < 5; i++)
            {
                if (i < rating)
                {
                    stars[i].Image = System.Drawing.Image.FromFile("D:\\大二下\\数据库编程\\花店管理系统\\花店管理系统\\bin\\Debug\\按键图标\\评分星1.jpg");
                }
                else
                {
                    stars[i].Image = System.Drawing.Image.FromFile("D:\\大二下\\数据库编程\\花店管理系统\\花店管理系统\\bin\\Debug\\按键图标\\评分星2.jpg");
                }
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (currentCommentIndex < commentsList.Count - 1)
            {
                currentCommentIndex++;
                UpdateCommentDisplay();
            }
        }

        private void btnP_Click(object sender, EventArgs e)
        {
            if (currentCommentIndex > 0)
            {
                currentCommentIndex--;
                UpdateCommentDisplay();
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnCanle_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
